<template>
	<view>
		<form>
			<view class="cu-form-group">
				<view class="title">资源名称</view>
				<input placeholder="名称" name="input" maxlength="128" v-model="resName"></input>
			</view>
			<view class="cu-form-group align-start">
				<view class="title">资源链接</view>
				<textarea maxlength="1024" auto-height="true" placeholder="链接" v-model="resLink"></textarea>
			</view>
			<!-- <view class="cu-form-group">
				<view class="title">是否公开</view>
				<switch class='blue' @change="switchType" :class="shareType == 1?'checked':''" :checked="shareType == 1?true:false"></switch>
			</view> -->
		</form>
		<view class="cu-bar bg-white justify-end">
			<view class="action">
				<button class="cu-btn light bg-blue text-green" @tap="cancel">取消</button>
				<button class="cu-btn bg-blue margin-left" @tap="save">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				resId: '',
				resName: '',
				resLink: '',
				shareType: 0
			}
		},
		methods: {
			cancel() {
				uni.navigateBack({});
			},
			switchType(e) {
				this.shareType = e.detail.value ? 1 : 0
			},
			save(){
				let that = this;
				uni.request({
					url: that.baseUrl + "/save",
					data: {
						resId: that.resId,
						userId: uni.getStorageSync('mini_sfshare_key'),
						resName: that.resName,
						resLink: that.resLink,
						shareType: that.shareType
					},
					success(res) {
						if (res.statusCode == 200) {
							uni.navigateBack({});
						} else {
							uni.showToast({
								icon: 'none',
								title: '保存资源失败，请重试！',
								duration: 1200
							});
						}
					},
					fail(res) {
						uni.showToast({
							icon: 'none',
							title: '服务器开了小差，请稍后再试！',
							duration: 1200
						});
					}
				});
			}
		},
		onLoad(query) {
			let that = this;
			if(query.id){
				uni.request({
					url: that.baseUrl + "/res",
					data: {
						resId: query.id
					},
					success(res) {
						if (res.statusCode == 200) {
							that.resId = res.data.UUID_;
							that.resName = res.data.RES_NAME_;
							that.resLink = res.data.RES_LINK_;
							that.shareType = res.data.SHARE_TYPE_;
						} else {
							uni.showToast({
								icon: 'none',
								title: '获取消息失败，请重试！',
								duration: 1200
							});
						}
					},
					fail(res) {
						uni.showToast({
							icon: 'none',
							title: '服务器开了小差，请稍后再试！',
							duration: 1200
						});
					}
				});
			}
		}
	}
</script>

<style>

</style>
